package fileidentifier.zipfileinformer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Created with IntelliJ IDEA.
 * User: ApT
 * To change this template use File | Settings | File Templates.
 */
public class ZipUtilites {
    private ZipFile zipFile;
    private List<ZipEntry> entryList;

    public ZipUtilites(String zipFileName) throws IOException {
        try {
            zipFile = new ZipFile(zipFileName);
            ZipFile zipFile = new ZipFile(zipFileName);
            Enumeration zipEntries = zipFile.entries();
            entryList = new ArrayList<ZipEntry>();
            while (zipEntries.hasMoreElements()) {
                entryList.add((ZipEntry) (zipEntries.nextElement()));
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public ZipFile getZipFile() {
        return zipFile;
    }

    public void setZipFile(ZipFile zipFile) {
        this.zipFile = zipFile;
    }

    public List getEntryList() {
        return entryList;
    }

    public void setEntryList(List entryList) {
        this.entryList = entryList;
    }

    public String getFileInfo() {
        String result = "";
        result += "ZIP file information\n";
        if (entryList == null) {
            return "This is not ZIP file";
        }
        for (ZipEntry zipEntry : entryList) {
            result += zipEntry.getName() + ":\n\tSize:\t\t" + zipEntry.getSize()
                    + "\n\tTime:\t\t" + Long.toString(zipEntry.getTime()) + "\n\tType:\t\t"
                    + checkDirrectory(zipEntry.isDirectory()) + "\n\tCRC:\t\t"
                    + zipEntry.getCrc() + "\n\tZIP size:\t" + zipEntry.getCompressedSize() + "\n";
        }
        return result;
    }

    private String checkDirrectory(boolean bool) {
        if (bool) {
            return "dirrectory";
        }
        return "file";
    }
}
